package cc.lj.internal.merchant.domain.contract.service.impl;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.merchant.domain.contract.model.vo.PropertyResourcesDO;
import cc.lj.internal.merchant.domain.contract.model.vo.convert.PropertyResourcesPOConvertDO;
import cc.lj.internal.merchant.domain.contract.repository.IMerchantContractObjectsRepository;
import cc.lj.internal.merchant.domain.contract.repository.IMerchantContractSignedRepository;
import cc.lj.internal.merchant.domain.contract.service.IPropertyResourcesDomainService;
import cc.lj.internal.merchant.infrastructure.po.MerchantContractObjectsPO;
import cc.lj.internal.merchant.infrastructure.po.MerchantContractSignedPO;
import cc.lj.internal.merchant.infrastructure.po.PropertyResourcesPO;
import cc.lj.internal.merchant.infrastructure.repository.PropertyResourcesRespository;
import lj.internal.dict.enums.EnumMerchantContractObject;
import lj.internal.dict.enums.EnumMerchantContractSignedStatus;
import lj.internal.dict.enums.EnumPropertyResourcesUseState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 物业资源展位表 domain service 实现
 * @author liangxifeng
 * @date 2021-12-25
 */
@Slf4j
@Service(value = "propertyResourcesDomainServiceImpl")
public class PropertyResourcesDomainServiceImpl implements IPropertyResourcesDomainService {
    //展位仓储接口
    @Resource(name = "propertyResourcesRespository")
    private PropertyResourcesRespository resourcesRespository;

    //合同签订表仓储接口
    @Resource(name = "merchantContractSignedRespository")
    private IMerchantContractSignedRepository contractSignedRepository;

    //收费账单标准表仓储接口
    @Resource(name = "merchantContractObjectsRespository")
    private IMerchantContractObjectsRepository contractObjectsRepository;


    @Override
    public PropertyResourcesDO getRentingResourceById(Integer id) {
        //通过主键查询展位表数据
        PropertyResourcesPO resourcesPO = resourcesRespository.getById(id);
        PropertyResourcesDO resourcesDO = PropertyResourcesPOConvertDO.INSTANCE.convert(resourcesPO);
        //如果该展位在租，则查询和计算该展位的日租金该展位
        if(resourcesDO.getResourcesUseState() == EnumPropertyResourcesUseState.RENTING.getCode()) {
            createShowDayRentPrice(resourcesDO);
        }
        return resourcesDO;
    }

    /**
     * 计算和生成在租展位日租金 在租展位租金单价*建筑面积
     * @param resourcesDO
     * @return 展位每日租金
     */
    public BigDecimal createShowDayRentPrice(PropertyResourcesDO resourcesDO) {
        //通过展位号和在租状态=1,查询该展位合同签订表数据
        MerchantContractSignedPO merchantContractSignedPO = new MerchantContractSignedPO();
        merchantContractSignedPO.setConResource(resourcesDO.getResourcesName()); //展位号
        merchantContractSignedPO.setContractState(EnumMerchantContractSignedStatus.RENTING.getCode()); //在租状态
        List<MerchantContractSignedPO> querySignedList = contractSignedRepository.getList(merchantContractSignedPO);
        if(querySignedList.size() == 0) {
            log.info("商户合同签订表数据通过展位号="+resourcesDO.getResourcesName()+"没有查询到数据");
            return null;
        }
        MerchantContractSignedPO querySignedPO = querySignedList.get(0);
        //log.info("展位号="+resourcesPO.getResourcesName()+",在租商户合同签订表数据PO="+querySignedPO);

        //通过合同签订表主键查询各收费项目表
        MerchantContractObjectsPO objectsPO = new MerchantContractObjectsPO();
        objectsPO.setObjectBeloneID(querySignedPO.getContractCid());
        objectsPO.setIsZu(EnumMerchantContractObject.IsZu.RENT.getCode()); //在租
        objectsPO.setObjectType(EnumMerchantContractObject.ObjectType.RENT.getCode());//租金费用
        objectsPO.setObjectState(EnumMerchantContractObject.ObjectState.ACTIVATE.getCode());//已生效
        List<MerchantContractObjectsPO> objectsPOList = contractObjectsRepository.getList(objectsPO);
        if(objectsPOList.size() == 0) {
            log.info("通过合同签订表主键"+querySignedPO.getContractCid()+",查询各收费项目表数据，无数据");
            return null;
        }
        //log.info("展位号="+resourcesPO.getResourcesName()+",在租展位租金收费项目表数据查询PO="+objectsPOList);
        //租金单价
        BigDecimal zuPrice = objectsPOList.get(0).getObjectPrice();
        //计算每日租金 = 在租展位租金单价*建筑面积
        resourcesDO.createRentRice(zuPrice);
        return resourcesDO.getDayRentPrice();
    }
}
